package com.yxt.qmgf.userinfo.ctrl;

import cn.hutool.extra.spring.SpringUtil;
import com.yxt.qmgf.common.view.CdzAlert;
import com.yxt.qmgf.start.dao.LoginDao;
import com.yxt.qmgf.start.vo.LoginVO;
import com.yxt.qmgf.userinfo.dao.UserInfoDao;
import com.yxt.qmgf.userinfo.vo.UserInfoVO;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.ResourceBundle;
@FXMLController
public class UserCreatController implements Initializable {
    @FXML
    private TextField tf_username;
    @FXML
    private TextField tf_password;
    @FXML
    private TextField tf_confirmPassword;//确认密码
    @FXML
    private TextField tf_name;
    @FXML
    private TextField tf_sex;//性别
    @FXML
    private TextField tf_code;//管理员（0：否，1：是）
    @FXML
    private TextField tf_score;//游戏积分

    private LoginVO vo;
    private Stage stage;

    @FXML
    private Label messageLabel;

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public UserCreatController(){
        super();
    }

    @FXML
    private void handleRegister() {
        LoginVO vo = new LoginVO();
        UserInfoVO uvo = new UserInfoVO();
        String username = tf_username.getText().trim();
        String password = tf_password.getText().trim();
        String name = tf_name.getText().trim();
        String confirmPassword = tf_confirmPassword.getText().trim();


        // 简单的验证
        if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() || name.isEmpty()) {
            messageLabel.setText("有未填项，请检查。");
            return;
        }

        if (!password.equals(confirmPassword)) {
            messageLabel.setText("确认密码与密码不匹配。");
            return;
        }
        int NUM = SpringUtil.getBean(LoginDao.class).SelectCount();
        vo.setUid(1001+ NUM);
        vo.setUsername(username);
        vo.setPassword(password);
        vo.setCode("0");
        vo.setName(name);
        SpringUtil.getBean(LoginDao.class).Insert(vo);

        //写入userinfo中
        uvo.setUsername(username);
        uvo.setCode("0");
        uvo.setName(name);
        uvo.setScore("0");
        uvo.setUid(String.valueOf(1001+ NUM));
        SpringUtil.getBean(UserInfoDao.class).InsertByVo(uvo);
        // 显示成功消息
        new CdzAlert(Alert.AlertType.INFORMATION, StringUtils.join("注册成功，请登录")).show();
        stage.close();
        // 可以选择清空字段或执行其他操作

    }


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }
}
